package com.alan.wifishare.view.impl.activity;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.alan.libapi.model.business.UserToken;
import com.alan.libapi.model.persistent.ModifyPwdType;
import com.alan.library.allog.KLog;
import com.alan.wifishare.R;
import com.alan.wifishare.presenter.UserVerifyIdentityPS;
import com.alan.wifishare.view.BaseActivity;
import com.alan.wifishare.view.impl.ActivityJumpUtil;
import com.alan.wifishare.view.iview.IVUserVerifyIdentity;

/**
 * @className: UserVerifyIdentityActivity
 * @describtion: UserVerifyIdentityActivity
 * @author: AlanWang
 * @email: 850267876@qq.com
 * @time: 2016/5/5 10:37
 */
public class UserVerifyIdentityActivity extends BaseActivity implements IVUserVerifyIdentity, View.OnClickListener{

    private UserVerifyIdentityPS userVerifyIdentityPS;
    private Button uerVerifyIdentityBackBtn;
    private EditText uerVerifyIdentityPhoneEt;
    private Button uerVerifyIdentitySendVcodeBtn;
    private EditText uerVerifyIdentityVerifyCodeEt;
    private Button uerVerifyIdentitySubmitBtn;
    private Handler timerHandler = new Handler();
    private Runnable timerRunable;
    private boolean isCanSendVerifyCode = true;
    private int remainSecond = 60;
    private ModifyPwdType modifyPwdType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_verify_identity_layout);
        userVerifyIdentityPS = new UserVerifyIdentityPS(this);
        initView();

        isCanSendVerifyCode = true;
        timerRunable = new Runnable() {
            @Override
            public void run() {
                remainSecond--;
                KLog.e("remainSecond = " + remainSecond);
                if(remainSecond <= 0){
                    remainSecond = 60;
                    timerHandler.removeCallbacks(this);
                    uerVerifyIdentitySendVcodeBtn.setText( "发送验证码");
                    isCanSendVerifyCode = true;
                }else {
                    uerVerifyIdentitySendVcodeBtn.setText(remainSecond + "S后重发");
                    uerVerifyIdentitySendVcodeBtn.postDelayed(timerRunable, 1000);
                }
            }
        };
    }

    @Override
    protected void initView() {
        uerVerifyIdentityBackBtn = (Button)findViewById(R.id.uerVerifyIdentityBackBtn);
        uerVerifyIdentityPhoneEt = (EditText)findViewById(R.id.uerVerifyIdentityPhoneEt);
        uerVerifyIdentitySendVcodeBtn = (Button)findViewById(R.id.uerVerifyIdentitySendVcodeBtn);
        uerVerifyIdentityVerifyCodeEt = (EditText)findViewById(R.id.uerVerifyIdentityVerifyCodeEt);
        uerVerifyIdentitySubmitBtn = (Button)findViewById(R.id.uerVerifyIdentitySubmitBtn);

        uerVerifyIdentityBackBtn.setOnClickListener(this);
        uerVerifyIdentitySendVcodeBtn.setOnClickListener(this);
        uerVerifyIdentitySubmitBtn.setOnClickListener(this);

        Bundle bundle = getIntent().getExtras();
        modifyPwdType = (ModifyPwdType) bundle.getSerializable(ActivityJumpUtil.Extra.MODIFY_PWD_TYPE);
        KLog.e(modifyPwdType);
    }

    @Override
    protected void onDestroy() {
        KLog.e();
        timerHandler.removeCallbacks(timerRunable);
        super.onDestroy();
    }

    @Override
    public ModifyPwdType getModifyType() {
        return modifyPwdType;
    }

    @Override
    public String getUserPhone() {
        return uerVerifyIdentityPhoneEt.getText().toString();
    }

    @Override
    public String getVerifyCode() {
        return uerVerifyIdentityVerifyCodeEt.getText().toString();
    }

    @Override
    public void getVerifyCodeSuccess() {
        isCanSendVerifyCode = false;
        timerHandler.removeCallbacks(timerRunable);
        timerHandler.postDelayed(timerRunable, 1000);
    }

    @Override
    public void verifySuccess(UserToken token) {
        KLog.e(token);
        ActivityJumpUtil.jump2UserModifyPwdAct(UserVerifyIdentityActivity.this, modifyPwdType, token);
        UserVerifyIdentityActivity.this.finish();
    }

    @Override
    public void showProgress() {
        progressDialogShow();
    }

    @Override
    public void hideProgress() {
        progressDialogHiden();
    }

    @Override
    public void toastMsg(String msg) {
        toastMsgShort(msg);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.uerVerifyIdentityBackBtn:
                this.finish();
                break;
            case R.id.uerVerifyIdentitySendVcodeBtn:
                KLog.e("isCanSendVerifyCode = " + isCanSendVerifyCode);
                if(isCanSendVerifyCode){
                    userVerifyIdentityPS.requestVerifyCode();
                }
                break;
            case R.id.uerVerifyIdentitySubmitBtn:
                userVerifyIdentityPS.sendVerifyCodeToServer();
                break;
            default:
                break;
        }
    }
}
